package com.ysstech.valuation.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.valuation.entity.Account;
import com.ysstech.valuation.entity.Valuation;
import com.ysstech.valuation.feign.FeignFinanceService;
import com.ysstech.valuation.service.ValuationService;
import com.ysstech.valuation.thread.ValuationMainGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-05-11
 */
@Controller
@Slf4j
@Api(tags = "估值表管理api")
@RequestMapping("/valuation")
public class ValuationController extends BaseController {
    @Autowired
    private ValuationService valuationService;
    @Autowired
    private ValuationMainGenerator valuationMainGenerator;
    @Autowired
    private FeignFinanceService feignFinanceService;

    @YssPermissions(value = {"valuation:strReport:valuation"})
    @ApiOperation(value = "跳转估值表管理页面")
    @YssLog(module = "估值表管理", remark = "跳转估值表管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "valuation/valuation";
    }

    @YssPermissions(value = {"valuation:strReport:valuation"})
    @ApiOperation(value = "获取估值表页面初始值")
    @YssLog(module = "估值表管理", remark = "获取估值表页面初始值")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<TreeNode> list = feignFinanceService.queryAccountTreeByType(redisCacheManager.getLoginUserInfo().getOrgId(),null,null);
        map.put("lsetlists", list);
        return ResultGenerator.getSuccessResult("获取页面初始化信息成功", map);
    }

    /**
     * 获取估值表信息
     *
     * @return
     */
    @YssPermissions(value = {"valuation:strReport:valuation"})
    @ApiOperation(value = "获取估值表信息")
    @YssLog(module = "估值表管理", remark = "获取估值表信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(Valuation valuation, String page, String rows) throws Exception {
        return ResultGenerator.getSuccessResult("获取估值表信息成功",
                valuationService.queryAllValuation(valuation, this.getPage(page, rows)));
    }

    /**
     * 生成估值表信息
     *
     * @return
     */
    @YssPermissions(value = {"valuation:strReport:valuation:produce"})
    @ApiOperation(value = "生成估值表信息")
    @YssLog(module = "估值表管理", remark = "生成估值表信息")
    @PostMapping(value = "/produceValuation")
    @ResponseBody
    public RestResult produceValuation(Valuation valuation) throws Exception {
        if (StringUtils.isBlank(valuation.getAccountId())) {
            throw new BusinessException("获取基金信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(valuation.getAccountId(), String.class);
        valuationMainGenerator.renewValuation(valuation, list);
        return ResultGenerator.getSuccessResult("生成估值表信息成功");
    }
}
